home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / traceback.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  11KB  |  364 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''Extract, format and print information about Python stack traces.'''
  5. import linecache
  6. import sys
  7. import types
  8. __all__ = [
  9.     'extract_stack',
  10.     'extract_tb',
  11.     'format_exception',
  12.     'format_exception_only',
  13.     'format_list',
  14.     'format_stack',
  15.     'format_tb',
  16.     'print_exc',
  17.     'format_exc',
  18.     'print_exception',
  19.     'print_last',
  20.     'print_stack',
  21.     'print_tb',
  22.     'tb_lineno']
  23.  
  24. def _print(file, str = '', terminator = '\n'):
  25.     file.write(str + terminator)
  26.  
  27.  
  28. def print_list(extracted_list, file = None):
  29.     '''Print the list of tuples as returned by extract_tb() or
  30.     extract_stack() as a formatted stack trace to the given file.'''
  31.     if file is None:
  32.         file = sys.stderr
  33.     for filename, lineno, name, line in extracted_list:
  34.         _print(file, '  File "%s", line %d, in %s' % (filename, lineno, name))
  35.         if line:
  36.             _print(file, '    %s' % line.strip())
  37.             continue
  38.  
  39.  
  40. def format_list(extracted_list):
  41.     '''Format a list of traceback entry tuples for printing.
  42.  
  43.     Given a list of tuples as returned by extract_tb() or
  44.     extract_stack(), return a list of strings ready for printing.
  45.     Each string in the resulting list corresponds to the item with the
  46.     same index in the argument list.  Each string ends in a newline;
  47.     the strings may contain internal newlines as well, for those items
  48.     whose source text line is not None.
  49.     '''
  50.     list = []
  51.     for filename, lineno, name, line in extracted_list:
  52.         item = '  File "%s", line %d, in %s\n' % (filename, lineno, name)
  53.         if line:
  54.             item = item + '    %s\n' % line.strip()
  55.         list.append(item)
  56.     
  57.     return list
  58.  
  59.  
  60. def print_tb(tb, limit = None, file = None):
  61.     """Print up to 'limit' stack trace entries from the traceback 'tb'.
  62.  
  63.     If 'limit' is omitted or None, all entries are printed.  If 'file'
  64.     is omitted or None, the output goes to sys.stderr; otherwise
  65.     'file' should be an open file or file-like object with a write()
  66.     method.
  67.     """
  68.     if file is None:
  69.         file = sys.stderr
  70.     if limit is None and hasattr(sys, 'tracebacklimit'):
  71.         limit = sys.tracebacklimit
  72.     
  73.     n = 0
  74.     while tb is not None:
  75.         if limit is None or n < limit:
  76.             f = tb.tb_frame
  77.             lineno = tb.tb_lineno
  78.             co = f.f_code
  79.             filename = co.co_filename
  80.             name = co.co_name
  81.             _print(file, '  File "%s", line %d, in %s' % (filename, lineno, name))
  82.             linecache.checkcache(filename)
  83.             line = linecache.getline(filename, lineno, f.f_globals)
  84.             if line:
  85.                 _print(file, '    ' + line.strip())
  86.             tb = tb.tb_next
  87.             n = n + 1
  88.         return None
  89.  
  90.  
  91. def format_tb(tb, limit = None):
  92.     """A shorthand for 'format_list(extract_tb(tb, limit))'."""
  93.     return format_list(extract_tb(tb, limit))
  94.  
  95.  
  96. def extract_tb(tb, limit = None):
  97.     """Return list of up to limit pre-processed entries from traceback.
  98.  
  99.     This is useful for alternate formatting of stack traces.  If
  100.     'limit' is omitted or None, all entries are extracted.  A
  101.     pre-processed stack trace entry is a quadruple (filename, line
  102.     number, function name, text) representing the information that is
  103.     usually printed for a stack trace.  The text is a string with
  104.     leading and trailing whitespace stripped; if the source is not
  105.     available it is None.
  106.     """
  107.     if limit is None and hasattr(sys, 'tracebacklimit'):
  108.         limit = sys.tracebacklimit
  109.     
  110.     list = []
  111.     n = 0
  112.     while tb is not None:
  113.         if limit is None or n < limit:
  114.             f = tb.tb_frame
  115.             lineno = tb.tb_lineno
  116.             co = f.f_code
  117.             filename = co.co_filename
  118.             name = co.co_name
  119.             linecache.checkcache(filename)
  120.             line = linecache.getline(filename, lineno, f.f_globals)
  121.             if line:
  122.                 line = line.strip()
  123.             else:
  124.                 line = None
  125.             list.append((filename, lineno, name, line))
  126.             tb = tb.tb_next
  127.             n = n + 1
  128.         return list
  129.  
  130.  
  131. def print_exception(etype, value, tb, limit = None, file = None):
  132.     '''Print exception up to \'limit\' stack trace entries from \'tb\' to \'file\'.
  133.  
  134.     This differs from print_tb() in the following ways: (1) if
  135.     traceback is not None, it prints a header "Traceback (most recent
  136.     call last):"; (2) it prints the exception type and value after the
  137.     stack trace; (3) if type is SyntaxError and value has the
  138.     appropriate format, it prints the line where the syntax error
  139.     occurred with a caret on the next line indicating the approximate
  140.     position of the error.
  141.     '''
  142.     if file is None:
  143.         file = sys.stderr
  144.     if tb:
  145.         _print(file, 'Traceback (most recent call last):')
  146.         print_tb(tb, limit, file)
  147.     lines = format_exception_only(etype, value)
  148.     for line in lines:
  149.         _print(file, line, '')
  150.     
  151.  
  152.  
  153. def format_exception(etype, value, tb, limit = None):
  154.     '''Format a stack trace and the exception information.
  155.  
  156.     The arguments have the same meaning as the corresponding arguments
  157.     to print_exception().  The return value is a list of strings, each
  158.     ending in a newline and some containing internal newlines.  When
  159.     these lines are concatenated and printed, exactly the same text is
  160.     printed as does print_exception().
  161.     '''
  162.     if tb:
  163.         list = [
  164.             'Traceback (most recent call last):\n']
  165.         list = list + format_tb(tb, limit)
  166.     else:
  167.         list = []
  168.     list = list + format_exception_only(etype, value)
  169.     return list
  170.  
  171.  
  172. def format_exception_only(etype, value):
  173.     '''Format the exception part of a traceback.
  174.  
  175.     The arguments are the exception type and value such as given by
  176.     sys.last_type and sys.last_value. The return value is a list of
  177.     strings, each ending in a newline.
  178.  
  179.     Normally, the list contains a single string; however, for
  180.     SyntaxError exceptions, it contains several lines that (when
  181.     printed) display detailed information about where the syntax
  182.     error occurred.
  183.  
  184.     The message indicating which exception occurred is always the last
  185.     string in the list.
  186.  
  187.     '''
  188.     if isinstance(etype, BaseException) and isinstance(etype, types.InstanceType) and etype is None or type(etype) is str:
  189.         return [
  190.             _format_final_exc_line(etype, value)]
  191.     stype = None.__name__
  192.     if not issubclass(etype, SyntaxError):
  193.         return [
  194.             _format_final_exc_line(stype, value)]
  195.     lines = None
  196.     
  197.     try:
  198.         (filename, lineno, offset, badline) = (msg,)
  199.     except Exception:
  200.         pass
  201.  
  202.     if not filename:
  203.         pass
  204.     filename = '<string>'
  205.     lines.append('  File "%s", line %d\n' % (filename, lineno))
  206.     if badline is not None:
  207.         lines.append('    %s\n' % badline.strip())
  208.         if offset is not None:
  209.             caretspace = badline.rstrip('\n')[:offset].lstrip()
  210.             caretspace = (lambda .0: pass)(caretspace)
  211.             lines.append('   %s^\n' % ''.join(caretspace))
  212.         
  213.     value = msg
  214.     lines.append(_format_final_exc_line(stype, value))
  215.     return lines
  216.  
  217.  
  218. def _format_final_exc_line(etype, value):
  219.     '''Return a list of a single line -- normal case for format_exception_only'''
  220.     valuestr = _some_str(value)
  221.     if value is None or not valuestr:
  222.         line = '%s\n' % etype
  223.     else:
  224.         line = '%s: %s\n' % (etype, valuestr)
  225.     return line
  226.  
  227.  
  228. def _some_str(value):
  229.     
  230.     try:
  231.         return str(value)
  232.     except Exception:
  233.         pass
  234.  
  235.     
  236.     try:
  237.         value = unicode(value)
  238.         return value.encode('ascii', 'backslashreplace')
  239.     except Exception:
  240.         pass
  241.  
  242.     return '<unprintable %s object>' % type(value).__name__
  243.  
  244.  
  245. def print_exc(limit = None, file = None):
  246.     """Shorthand for 'print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, limit, file)'.
  247.     (In fact, it uses sys.exc_info() to retrieve the same information
  248.     in a thread-safe way.)"""
  249.     if file is None:
  250.         file = sys.stderr
  251.     
  252.     try:
  253.         (etype, value, tb) = sys.exc_info()
  254.         print_exception(etype, value, tb, limit, file)
  255.     finally:
  256.         etype = None
  257.         value = None
  258.         tb = None
  259.  
  260.  
  261.  
  262. def format_exc(limit = None):
  263.     '''Like print_exc() but return a string.'''
  264.     
  265.     try:
  266.         (etype, value, tb) = sys.exc_info()
  267.         return ''.join(format_exception(etype, value, tb, limit))
  268.     finally:
  269.         etype = None
  270.         value = None
  271.         tb = None
  272.  
  273.  
  274.  
  275. def print_last(limit = None, file = None):
  276.     """This is a shorthand for 'print_exception(sys.last_type,
  277.     sys.last_value, sys.last_traceback, limit, file)'."""
  278.     if not hasattr(sys, 'last_type'):
  279.         raise ValueError('no last exception')
  280.     if file is None:
  281.         file = sys.stderr
  282.     print_exception(sys.last_type, sys.last_value, sys.last_traceback, limit, file)
  283.  
  284.  
  285. def print_stack(f = None, limit = None, file = None):
  286.     """Print a stack trace from its invocation point.
  287.  
  288.     The optional 'f' argument can be used to specify an alternate
  289.     stack frame at which to start. The optional 'limit' and 'file'
  290.     arguments have the same meaning as for print_exception().
  291.     """
  292.     if f is None:
  293.         
  294.         try:
  295.             raise ZeroDivisionError
  296.         except ZeroDivisionError:
  297.             f = sys.exc_info()[2].tb_frame.f_back
  298.         
  299.  
  300.     print_list(extract_stack(f, limit), file)
  301.  
  302.  
  303. def format_stack(f = None, limit = None):
  304.     """Shorthand for 'format_list(extract_stack(f, limit))'."""
  305.     if f is None:
  306.         
  307.         try:
  308.             raise ZeroDivisionError
  309.         except ZeroDivisionError:
  310.             f = sys.exc_info()[2].tb_frame.f_back
  311.         
  312.  
  313.     return format_list(extract_stack(f, limit))
  314.  
  315.  
  316. def extract_stack(f = None, limit = None):
  317.     """Extract the raw traceback from the current stack frame.
  318.  
  319.     The return value has the same format as for extract_tb().  The
  320.     optional 'f' and 'limit' arguments have the same meaning as for
  321.     print_stack().  Each item in the list is a quadruple (filename,
  322.     line number, function name, text), and the entries are in order
  323.     from oldest to newest stack frame.
  324.     """
  325.     if f is None:
  326.         
  327.         try:
  328.             raise ZeroDivisionError
  329.         except ZeroDivisionError:
  330.             f = sys.exc_info()[2].tb_frame.f_back
  331.         
  332.  
  333.     if limit is None and hasattr(sys, 'tracebacklimit'):
  334.         limit = sys.tracebacklimit
  335.     
  336.     list = []
  337.     n = 0
  338.     while f is not None:
  339.         if limit is None or n < limit:
  340.             lineno = f.f_lineno
  341.             co = f.f_code
  342.             filename = co.co_filename
  343.             name = co.co_name
  344.             linecache.checkcache(filename)
  345.             line = linecache.getline(filename, lineno, f.f_globals)
  346.             if line:
  347.                 line = line.strip()
  348.             else:
  349.                 line = None
  350.             list.append((filename, lineno, name, line))
  351.             f = f.f_back
  352.             n = n + 1
  353.         list.reverse()
  354.         return list
  355.  
  356.  
  357. def tb_lineno(tb):
  358.     '''Calculate correct line number of traceback given in tb.
  359.  
  360.     Obsolete in 2.3.
  361.     '''
  362.     return tb.tb_lineno
  363.  
  364.